// view.js
wx.cloud.init()
const db = wx.cloud.database()
Page({
  data: {
    banner: [],
    navCate: [],
    recommendationList: [],
    newShopList: [],
    search: [],
    searchText: "",
    num: 0,
    // useArr:[],
    // mathArr:[]
  },
  
  

  
  onLoad() {
    let that = this
   
    // 首页风景列表
    db.collection("recommendation").skip(that.data.num).limit(10).get({
      success: function (res) {
        console.log("风景列表获取成功", res);
        that.setData({
            recommendationList: res.data
        })
        for (let i = 0,len=that.data.recommendationList.length; i <len ; i++) {
          let currentRandom = parseInt(Math.random() * (that.data.recommendationList.length - 1));
          console.log(currentRandom);
          let current = that.data.recommendationList[i];
          that.data.recommendationList[i] = that.data.recommendationList[currentRandom];
          that.data.recommendationList[currentRandom] = current;
        }
        that.setData({
            recommendationList: that.data.recommendationList
        })
      },
      fail: function (res) {
        console.log("风景列表获取失败", res);
      }
    })
  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    let that = this
    wx.showLoading({
      title: '加载中',
      duration: 1000
    })
    that.setData({
      num: (that.data.num + 10)
    })
    console.log(that.data.num);
    db.collection("recommendation").skip(that.data.num).limit(10).get({
      success: function (res) {
        console.log("风景列表获取成功", res);
        that.setData({
            recommendationList: that.data.recommendationList.concat(res.data)
        })
        console.log(that.data.recommendationList.length);
        console.log(that.data.num);
        console.log(parseInt(Math.random() * (that.data.recommendationList.length - 1)+that.data.num));
        for (let i = that.data.num,len=that.data.recommendationListt.length; i <len ; i++) {
          let currentRandom = parseInt(Math.random() * (that.data.recommendationList.length - 1-that.data.num)+that.data.num);
          let current = that.data.recommendationList[i];
          that.data.recommendationList[i] = that.data.recommendationList[currentRandom];
          that.data.recommendationList[currentRandom] = current;
        }
        that.setData({
            recommendationList: that.data.recommendationList
        })
      },
      fail: function (res) {
        console.log("风景列表获取失败", res);
      }
    })
  },


})
